using System;
using UnityEngine;

public class EnergyControl : MonoBehaviour
{
	public BarSliderControl energyBar;

	public UILabel energyValues;

	public PanelLayerControl panelControl;

	private int prevEnergiValue;

	public UISprite energySprite;

	private bool canUpdate = true;

	public string energyFormatString
	{
		get
		{
			return "{0}/" + EnergyControlConfig.instance.maxEnergy;
		}
	}

	private void OnEnable()
	{
		UpdateEnergy(true);
		energyBar.UpdateSlider(Mathf.Min(BehaviourSingleton<EnergyManager>.instance.GetCurrentEnergyPercent(), 1f));
	}

	private void UpdateEnergy(bool force)
	{
		int num = (int)BehaviourSingleton<EnergyManager>.instance.GetCurrentEnergyValue();
		if (prevEnergiValue != num || force)
		{
			string text = string.Format(energyFormatString, num);
			UITools.ChangeText(energyValues, text);
			prevEnergiValue = num;
		}
	}

	private void Update()
	{
		if (canUpdate)
		{
			UpdateEnergy(false);
			float progress = Mathf.Lerp(energyBar.fillAmount, Mathf.Min(BehaviourSingleton<EnergyManager>.instance.GetCurrentEnergyPercent(), 1f), 5f * RealTime.deltaTime);
			energyBar.UpdateSlider(progress);
		}
	}

	public void OnClickAddEnergy()
	{
		EnergyDialog.instance.Show();
	}

	private void PushRefillMenu()
	{
		EnergyRefillDialog.instance.ShowOwnedEnergyRefills();
	}

	private void PushShop()
	{
		TableTennisMainNavigation tableTennisMainNavigation = NavigationManager.instance as TableTennisMainNavigation;
		tableTennisMainNavigation.PushItemShop(StoreItemsConfig.StoreItem.ItemType.Food);
	}

	public void AnimateChangeEnergy(UIWidget visibleWidget, float fromValue, float toValue, Action onComplete, float duration = 0.5f)
	{
		float num = fromValue / EnergyControlConfig.instance.maxEnergy;
		float num2 = toValue / EnergyControlConfig.instance.maxEnergy;
		int fromIntValue = (int)fromValue;
		int toIntValue = (int)toValue;
		canUpdate = false;
		panelControl.ShowParticlesOnFill(visibleWidget, energySprite, energyBar.slider, energyValues, fromIntValue, toIntValue, energyFormatString, fromValue, toValue, energySprite.spriteName, duration, delegate
		{
			canUpdate = true;
			if (onComplete != null)
			{
				onComplete();
			}
		});
	}

	public void AnimateSpendEnergy(UIWidget visibleWidget, float spent, Action onComplete, float duration = 0.5f)
	{
		EnergyManager instance = BehaviourSingleton<EnergyManager>.instance;
		float currentEnergyValue = instance.GetCurrentEnergyValue();
		float num = Mathf.Max(0f, currentEnergyValue - spent);
		float fromValue = currentEnergyValue / EnergyControlConfig.instance.maxEnergy;
		float toValue = num / EnergyControlConfig.instance.maxEnergy;
		int fromIntValue = (int)currentEnergyValue;
		int toIntValue = (int)num;
		canUpdate = false;
		panelControl.ShowParticlesOnFill(visibleWidget, energySprite, energyBar.slider, energyValues, fromIntValue, toIntValue, energyFormatString, fromValue, toValue, energySprite.spriteName, duration, delegate
		{
			canUpdate = true;
			if (onComplete != null)
			{
				onComplete();
			}
		});
	}

	public void AnimateSpendEnergyAlreadySpent(UIWidget visibleWidget, float spent, Action onComplete, float duration = 0.5f)
	{
		EnergyManager instance = BehaviourSingleton<EnergyManager>.instance;
		float num = instance.GetCurrentEnergyValue() + spent;
		float currentEnergyValue = instance.GetCurrentEnergyValue();
		float fromValue = num / EnergyControlConfig.instance.maxEnergy;
		float toValue = currentEnergyValue / EnergyControlConfig.instance.maxEnergy;
		int fromIntValue = (int)num;
		int toIntValue = (int)currentEnergyValue;
		canUpdate = false;
		panelControl.ShowParticlesOnFill(visibleWidget, energySprite, energyBar.slider, energyValues, fromIntValue, toIntValue, energyFormatString, fromValue, toValue, energySprite.spriteName, duration, delegate
		{
			canUpdate = true;
			if (onComplete != null)
			{
				onComplete();
			}
		});
	}
}
